import requests
from lxml import html
import json
import os

# 构建JSON文件路径（在当前工作目录下）
json_file_path = 'movie.json'

# 检查文件是否存在
if not os.path.exists(json_file_path):
    # 如果文件不存在，则创建文件并写入空列表[]
    with open(json_file_path, 'w') as file:
        json.dump([], file)

# 读取JSON文件
with open('movie.json', 'r', encoding='utf-8') as file:
    data = json.load(file)
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
# 遍历每个章节
for chapter in data:
    chapter_name = chapter['name']
    chapter_url = chapter['url']
    current_page = 1  # 当前页数

    chapter_added = False  # 标志检查是否已为当前URL添加了chapter_name

    chapter_content = ''  # 初始化章节内容变量

    while True:
        # 构造当前页面的URL
        current_url = chapter_url.rstrip('.html') + f'_{current_page}.html'
        print(current_url)

        # 发送HTTP请求
        response = requests.get(current_url, headers=headers)

        # 检查请求是否成功
        if response.status_code == 200:
            # 使用lxml解析HTML内容
            tree = html.fromstring(response.text)

            # 使用XPath定位章节内容
            content = tree.xpath('//div[@id="content"]//p/text()')

            # full_content = ''.join(content).strip()
            # 将 \r 替换为 \n 并清除格式
            formatted_content = [paragraph.replace('\r', '\n').strip() for paragraph in content]

            # 使用换行符 '\n' 连接段落
            full_content = '\n'.join(formatted_content)

            # 如果找到内容，写入到文本文件中
            if full_content:
                if not chapter_added:
                    chapter_content += f'{chapter_name}\n'
                    chapter_added = True

                chapter_content += f'{full_content}\n'
            else:
                # with open('被关十万年，我疯了，也无敌了.txt', 'a', encoding='utf-8') as output_file:
                with open('反派：谁说我是来退婚的.txt', 'a', encoding='utf-8') as output_file:
                    output_file.write(f'未找到内容：{chapter_name}\n')

            # 检查是否有下一页按钮
            next_page_button = tree.xpath('//*[@id="next_url"]/text()')
            if not next_page_button or next_page_button[0].strip() != '下一页':
                break  # 没有下一页，结束循环
            else:
                current_page += 1  # 继续下一页
        else:
            with open('反派：谁说我是来退婚的.txt', 'a', encoding='utf-8') as output_file:
                output_file.write(f'无法访问章节: {chapter_name}，状态码: {response.status_code}\n')

    if chapter_added:
        # chapter_content += '=' * 50 + '\n'
        with open('反派：谁说我是来退婚的.txt', 'a', encoding='utf-8') as output_file:
            output_file.write(chapter_content)
